package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.ClientPermissionCacheDataDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RouterService {

    private static final Logger logger = LoggerFactory.getLogger(RouterService.class);

    @Autowired
    private RouterSmsService routerSmsService;

    @Autowired
    private RouterLogService routerLogService;

    @Autowired
    private RouterMeiTuanShanGouService routerMeiTuanShanGouService;

    @Autowired
    private RouterHhrErpService routerHhrErpService;

    @Autowired
    private RouterKuaiShouLifeService routerKuaiShouLifeService;

    @Autowired
    private RouterOrderService routerOrderService;

    @Autowired
    private RouterDouyinLifeService routerDouyinLifeService;

    @Autowired
    private RouterMemberService routerMemberService;

    @Autowired
    private RouterStoreService routerStoreService;

    @Autowired
    private RouterElemeRetailService routerElemeRetailService;

    @Autowired
    private RouterDingDingExchangeService routerDingDingExchangeService;

    @Autowired
    private RouterThirdService routerThirdService;

    @Autowired
    private RouterJingdongHomeService routerJingdongHomeService;

    /**
     * 入口服务
     *
     * @param clientInfoDTO 缓存中的客户端信息
     * @param clientKey 客户端key
     * @param clientSecret 客户端secret
     * @param data 请求参数
     * @return
     */
    public Object execute(ClientPermissionCacheDataDTO clientInfoDTO, String clientKey, String clientSecret, String cmd, JSONObject data)  throws Exception {

        // 校验
        if(cmd == null || "".equals(cmd)){
            throw new DefaultException("cmd参数不能为空");
        }

        // 路由到 短信 相关子路由
        if(cmd.startsWith(EntranceConstant.ROUTER_SMS_PREFIX)){
            logger.info("当前请求为短信请求...");
            return routerSmsService.execute(clientKey, clientSecret, cmd, data);
        }

        // 路由到 日志 相关子路由
        if(cmd.startsWith(EntranceConstant.ROUTER_LOG_PREFIX)){
            logger.info("当前请求为日志请求...");
            return routerLogService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        // 路由到 美团闪购 相关子路由
        if(cmd.startsWith(EntranceConstant.ROUTER_MEITUAN_SHANGOU_PREFIX)){
            logger.info("当前请求为美团闪购请求...");
            return routerMeiTuanShanGouService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        // 路由到 合伙人ERP 相关子路由
        if(cmd.startsWith(EntranceConstant.ROUTER_HHRERP_PREFIX)){
            logger.info("当前请求为合伙人ERP请求...");
            return routerHhrErpService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        // 路由到 快手本地生活 相关子路由
        if(cmd.startsWith(EntranceConstant.ROUTER_KUAISHOU_PREFIX)){
            logger.info("当前请求为快手本地生活请求...");
            return routerKuaiShouLifeService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        // 路由到 订单 相关子路由
        if(cmd.startsWith(EntranceConstant.ROUTER_ORDER_PREFIX)){
            logger.info("当前请求为订单请求...");
            return routerOrderService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        // 路由到 抖音本地生活 相关子路由
        if(cmd.startsWith(EntranceConstant.ROUTER_DOUYIN_LIFE_PREFIX)){
            logger.info("当前请求为快手本地生活请求...");
            return routerDouyinLifeService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        // 路由到 会员 相关子路由
        if(cmd.startsWith(EntranceConstant.ROUTER_MEMBER_PREFIX)){
            logger.info("当前请求为会员请求...");
            return routerMemberService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        // 路由到 门店 相关子陆游
        if(cmd.startsWith(EntranceConstant.ROUTER_STORE_PREFIX)){
            logger.info("当前请求为门店请求...");
            return routerStoreService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        // 路由到 饿了么零售 相关子陆游
        if(cmd.startsWith(EntranceConstant.ROUTER_ELEME_RETAIL_PREFIX)){
            logger.info("当前请求为饿了么零售请求...");
            return routerElemeRetailService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        // 路由到 钉钉数据交互 相关子陆游
        if(cmd.startsWith(EntranceConstant.ROUTER_DINGDING_EXCHANGE_PREFIX)){
            logger.info("当前请求为钉钉数据交互请求...");
            return routerDingDingExchangeService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        // 路由到 三方服务 相关子路由
        if(cmd.startsWith(EntranceConstant.ROUTER_THIRD_PREFIX)){
            logger.info("当前请求为三方服务交互请求...");
            return routerThirdService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        // 路由到 京东到家 相关子路由
        if(cmd.startsWith(EntranceConstant.ROUTER_JINGDONG_HOME_PREFIX)){
            logger.info("当前请求为京东到家服务交互请求...");
            return routerJingdongHomeService.execute(clientInfoDTO, clientKey, clientSecret, cmd, data);
        }

        throw new DefaultException("未找到此服务或服务不完整");
    }

}
